package edu.toronto.whimper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import edu.toronto.whimper.DataProcessor.DBAdapter;
import edu.toronto.whimper.DataProcessor.DBManager;
import edu.toronto.whimper.GeneralObjects.MyLocation;
import edu.toronto.whimper.GeneralObjects.NoiseSample;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;
import java.io.*;

public class Utils {
	
	public static boolean mExternalStorageAvailable = false;
	public static boolean mExternalStorageWriteable = false;
	
	public static void checkExternalStorageAvailability()
	{
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		
  	}
	
	public static ArrayList<MyLocation> getdumpLocations()
	{
		ArrayList<MyLocation> list = new ArrayList<MyLocation>();
		
		//list.add(new MyLocation(43.65367, -79.391022));// 105 mcCaul
		//list.add(new MyLocation(43.671219, -79.418198)); // 68 Vermont
		//list.add(new MyLocation(43.681921, -79.431251)); //32 Atlas
		
		list.add(new MyLocation(43.664804, -79.394803)); 
		list.add(new MyLocation(43.664494, -79.396359)); 
		list.add(new MyLocation(43.664067, -79.398365)); 
		list.add(new MyLocation(43.662980, -79.397957)); 
		list.add(new MyLocation(43.662165, -79.397657)); 
		
		return list;
	}
	
	public static String getNowDateTime()
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
	
	public static Date getDateTimeFromStr(String s, DateFormat df)
    {
		Date date = new Date();
		try
		{
			date = df.parse(s);  
		}		
		catch (ParseException e)
		{   			
			e.printStackTrace();    		
		}
		
		return date;
    }
	public static String getTodaysDate()
    {
		 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	        Date date = new Date();
	        return dateFormat.format(date);
	}
	public static enum MarkerColors{
	    BROWN, RED, YELLOW, GREEN, 
	    BLUE, PURPLE 
	}
	public static void SaveToFile(Context cont, ArrayList<NoiseSample> allSamples)
	{
		try {
			
			Writer writer = null;

		    File file2 = new File(Environment.getExternalStorageDirectory(),"data");
		    writer = new BufferedWriter(new FileWriter(file2));
		    
		    for (int i = 0; i < allSamples.size(); i++) {
		    	writer.write(allSamples.get(i).toString() + "\n");
			}
		    
		    writer.close();
		    
		     Toast.makeText(cont,"The file: " + "data" + " was saved" , Toast.LENGTH_LONG).show();
		    } 
		catch(IOException e)
	    {
		  e.printStackTrace();
	      Toast.makeText(cont, e.toString(), Toast.LENGTH_SHORT).show();
	    }
	}
	
	public static void LoadToDb(Context cont)
	{
		DBAdapter db = new DBAdapter(cont);
				
		 try{
		    FileInputStream fstream = new FileInputStream(Environment.getExternalStorageDirectory()+"/data");
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;

		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)   {
		      // Print the content on the console
		    	
		    	String[]data = strLine.split(",");
		    	//NoiseSample sample = new NoiseSample(data[0],Double.parseDouble(data[1]),new MyLocation(Double.parseDouble(data[2]),Double.parseDouble(data[3])));
		    	
		    	DBManager.insertNoiseSample(db, data[0], Double.parseDouble(data[1]), Double.parseDouble(data[2]), Double.parseDouble(data[3]), 0.0);
		    	
		    	//allSamples.add(sample);
		    }
		    in.close();
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		    }
		        
	}
			
	public static String arrayToString(ArrayList<NoiseSample> stringArray)
	{		   
		String str = "";
	    for (int i = 0; i < stringArray.size(); i++) {
	      str = str + stringArray.get(i).toString();
	    }
	    return str;
	  }
	
	
	
}

